package info.sean.util;


import info.sean.logger.GmfLogger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

import org.aspectj.lang.annotation.*;

import java.util.Arrays;


@Aspect
@Component
public class LogUtil {

    private static Logger logger = GmfLogger.getLogger(LogUtil.class);

    @Pointcut("execution(* com.ance.consumerfinance.lsc.job.biz.batch..*.*(com.ance.consumerfinance.lsc.job.biz.job1.dispatcher.*))")
    public void methodPointcut() {
    }

    // 环绕通知
    @Around("methodPointcut()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        String declaringTypeName = joinPoint.getSignature().getDeclaringTypeName();
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        try {
            logger.info("{}:方法开始执行,开始时间:{},入参:{}",declaringTypeName+"."+name,start,Arrays.asList(args));
            Object result = joinPoint.proceed(args);
            long end = System.currentTimeMillis();
            logger.info("{}:方法执行结束,结束时间:{},返回值:{},耗时:{}ms",declaringTypeName+"."+name,end,result,(end-start));
            return result;
        } catch (Throwable e) {
            long end = System.currentTimeMillis();
            logger.error("{}:方法执行异常,结束时间:{},异常信息:{},耗时:{}ms",declaringTypeName+"."+name,end,e,(end-start));
            throw e;
        }
    }
}
